home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group01b.txt / 000013_icon-group-sender _Mon Jul 24 09:07:26 2000.msg < prev    next >
Internet Message Format  |  2002-01-03  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id JAA07485
  4.     for icon-group-addresses; Mon, 24 Jul 2000 09:07:17 -0700 (MST)
  5. Message-Id: <200007241607.JAA07485@baskerville.CS.Arizona.EDU>
  6. Date: Mon, 24 Jul 2000 08:24:06 -0700
  7. From: Steve Wampler <swampler@noao.edu>
  8. X-Accept-Language: en
  9. To: Todd Nathan <todd@palomablanca.net>,
  10.         icon-group <icon-group@optima.CS.Arizona.EDU>
  11. Subject: Re: deep write()?
  12. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  13. Status: RO
  14. Content-Length: 2276
  15.  
  16. Todd Nathan wrote:
  17. > Is there a deep write method.
  18. > Just come came across what I suspect there to be a weakness
  19. > inherent in the function write().  No knowing how to do repeated
  20. > recursive diggin, how would someone write a deep write...
  21. > The following fails.
  22. > procedure main ()
  23. >  car1 := ["buick", "skylark", 1978, 2450]
  24. >  car2 := ["bmw", "535is", 1987, 3000]
  25. >  inventory := [car1, car2]
  26. >  write (inventory)
  27. > end
  28.  
  29. When write() encounters an argument that can't be converted to
  30. type string, it uses the image() function to display it.  What
  31. you're really asking for is a "deep" image() function.
  32.  
  33. Because Icon structures can contain *anything* including
  34. references to themselves, a deep image is non-trivial, though
  35. possible.
  36.  
  37. Consider, for example, what the output should be if the
  38. above line:
  39.  
  40.     inventory := [car1, car2]
  41.  
  42. were followed by
  43.  
  44.     put(inventory, inventory)
  45.  
  46. (perfectly legal code, if meaningless in this context...)
  47.  
  48. What should the deep image (and hence write()) produce?
  49.  
  50. A general purpose deep image has got to be able to handle the above.
  51. It's probably simpler to write a special-purpose one yourself
  52. if you are *absolutely* certain your structures are not cyclic graphs.
  53.  
  54. > I would like to see the 'write (inventory)' work as a deep
  55. > write....
  56.  
  57. Well, here's one to deep write a list, assuming the list is
  58. acyclic.  (Handling a cyclic structure is left as an exercise... :)
  59.  
  60. procedure deepWrite(args[])
  61.    outfile := &output
  62.    every arg := !args do {
  63.       case type(arg) of {
  64.           "file": outfile := arg
  65.           default:  writes(outfile, deepImage(arg))
  66.           }
  67.       }
  68.    write(outfile)
  69. end
  70.  
  71. procedure deepImage(arg)
  72.    local resultString
  73.  
  74.    if string(arg) then return string(arg)
  75.    case type(arg) of {
  76.        "list": {
  77.             resultString := "["
  78.             every resultString ||:= deepImage(!arg) || ","
  79.             resultString[-1] := "]"     # change last , to ]
  80.             return resultString
  81.             }
  82.        default: return image(arg)       # cop out on other structures
  83.        }
  84.  
  85. end
  86.  
  87.  
  88. Note that this code *depends* on any list passed to deepImage as being
  89. acyclic - it *won't* work on cyclic structures!
  90.  
  91. --
  92. Steve Wampler-  SOLIS Project, National Solar Observatory
  93. swampler@noao.edu
  94.